/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
using System;
using System.Collections;
namespace Velocity4Net.Runtime.Logging
{
    /**
     *  Pre-init logger.  I believe that this was suggested by
     *  Carsten Ziegeler <cziegeler@sundn.de> and
     *  Jeroen C. van Gelderen.  If this isn't correct, let me
     *  know as this was a good idea...
     *
     * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
     * @author <a href="mailto:nbubna@apache.org">Nathan Bubna</a>
     * @version $Id: HoldingLogChute.java 730039 2008-12-30 03:53:19Z byron $
     * @since 1.5
     */
    class HoldingLogChute : LogChute
    {
        private ArrayList pendingMessages = new ArrayList();
        private volatile bool transferring = false;

        /**
         * @see org.apache.velocity.runtime.LogChute#init(org.apache.velocity.runtime.RuntimeServices)
         */
        public void init(RuntimeServices rs)
        {
        }

        /**
         * Logs messages. All we do is store them until 'later'.
         *
         * @param level severity level
         * @param message complete error message
         */
        public void log(int level, String message)
        {
            if (!transferring)
            {
                Object[] data = new Object[2];
                data[0] = level;
                data[1] = message;
                pendingMessages.Add(data);
            }
        }

        /**
         * Logs messages and errors. All we do is store them until 'later'.
         *
         * @param level severity level
         * @param message complete error message
         * @param t the accompanying java.lang.Throwable
         */
        public void log(int level, String message, System.Exception t)
        {
            if (!transferring)
            {
                Object[] data = new Object[3];
                data[0] = level;
                data[1] = message;
                data[2] = t;
                pendingMessages.Add(data);
            }
        }

        /**
         * @see org.apache.velocity.runtime.LogChute#isLevelEnabled(int)
         */
        public bool isLevelEnabled(int level)
        {
            return true;
        }

        /**
         * Dumps the log messages this chute is holding into a new chute
         * @param newChute
         */
        public void transferTo(LogChute newChute)
        {
            if (!transferring && pendingMessages.Count != 0)
            {
                // let the other methods know what's up
                transferring = true;

                // iterate and log each individual message...
                foreach (Object[] data in pendingMessages)
                {
                    int level = ((int)data[0]);
                    String message = (String)data[1];
                    if (data.Length == 2)
                    {
                        newChute.log(level, message);
                    }
                    else
                    {
                        newChute.log(level, message, (System.Exception)data[2]);
                    }
                }
            }
        }

    }
}